import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sptapp/data/user_info_entity.dart';
import 'package:sptapp/moudle/common_info.dart';

class LocalBaseInfo {
  static SharedPreferences? sharedPreferences;
  static Future<void> init() async {
    sharedPreferences = await SharedPreferences.getInstance();
  }

  static String? getUserId() {
    return sharedPreferences?.getString('userId');
  }

  static  setUserId(String uid) {
     sharedPreferences?.setString('userId',uid);
  }

  static int getPlayTime(String itemId) {
    int duration = sharedPreferences?.getInt(itemId) ?? 0;
    print('getPlayTime ==== $duration');
    return duration;
  }

  static  setPlayTime(String itemId,int duration) {
    print('setPlayTime ==== $itemId $duration');
    sharedPreferences?.setInt(itemId,duration);
  }

  static setAccount(String account) {
     sharedPreferences?.setString('account',account);
  }

  static String?  getAccount() {
    return sharedPreferences?.getString('account');
  }

  static setDeviceNum(String deviceNum) {
    sharedPreferences?.setString('deviceNum',deviceNum);
  }

  static String?  getDeviceNum() {
    return sharedPreferences?.getString('deviceNum');
  }

  static setToken(String token) {
    sharedPreferences?.setString('token',token);
  }

  static String?  getToken() {
    return sharedPreferences?.getString('token');
  }

  static setPassword(String password) {
    sharedPreferences?.setString('password',password);
  }

  static String?  getPassword() {
    return sharedPreferences?.getString('password');
  }

  static setWifiName(String wifiName)  {
    sharedPreferences?.setString('wifiName',wifiName);
  }

  static String?  getWifiName() {
    return sharedPreferences?.getString('wifiName');
  }

  static clearInfo(){
    baseDevices = [];
    baseDeviceNum = null;
    baseDevice = null;
    baseUserInfo = {};
    sharedPreferences?.remove("token");
  }


  //本地数据缓存
  static Future<void> setSearchKeWords(String words)async {
    String? listStr = sharedPreferences?.getString('searchKeWords');
    List arr = [];
    if(listStr!=null){
      arr = jsonDecode(listStr);
      if(arr.contains(words)){
        arr.remove(words);
      }
      if(arr.length>6){
        arr.removeAt(6);
      }
    }
    arr.insert(0,words);
    await sharedPreferences?.setString('searchKeWords',jsonEncode(arr));
  }

  static List getSearchKeWords() {
    List arr = [];
    String? listStr = sharedPreferences?.getString('searchKeWords');
    if(listStr!=null){
      arr = jsonDecode(listStr);
    }
    print('arr == $arr');
    return arr;
  }

  static void clearSearchKeWords() {
    sharedPreferences?.remove('searchKeWords');
  }
}
